/**
 *
 * @author Roninwz
 * @date 2019/1/14 下午15:15
 * @since 1.0.0
 */

/**
 *  判断两个对象是否相等
 * @param obj1
 * @param obj2
 * @returns {boolean}
 */
function diffObj(obj1, obj2) {
    let o1 = obj1 instanceof Object;
    let o2 = obj2 instanceof Object;
    if (!o1 || !o2) {/*  判断不是对象  */
        return obj1 === obj2;
    }

    if (Object.keys(obj1).length !== Object.keys(obj2).length) {
        return false;
        //Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如：数组返回下表：let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
    }

    for (let attr in obj1) {
        let t1 = obj1[attr] instanceof Object;
        let t2 = obj2[attr] instanceof Object;
        if (t1 && t2) {
            return diffObj(obj1[attr], obj2[attr]);
        } else if (obj1[attr] !== obj2[attr]) {
            return false;
        }
    }
    return true;
}


/**
 *  字符串反转
 * @param str
 * @returns {*}
 */
function reverseStr(str) {
    if (str === null || str === '') {
        return str;
    }
    return str.split("").reverse().join("");
}

/**
 *  判断字符串是否为空或者空串
 * @param str
 * @returns {boolean}
 */
function isEmptyStrstr() {

    return str === null || str === '';
}

/**
 * 判断一个字符串是否包含另一个字符串
 * @param conStr  长字符串
 * @param conStrEd  短字符串  被包含
 * @returns {boolean}
 */
function isContainStr(conStr, conStrEd) {
    if (conStr.indexOf(conStrEd) !== -1) {
        return true
    } else {
        return false;
    }
}


/**
 * 生成指定范围内的随机数  可以等于最大或最小数
 * @param max  最大
 * @param min  最小
 * @returns {*}
 */
function randomRangeInt(max, min) {
    min = min || 0;
    // Math.floor  向下取整
    let result = min + Math.floor(Math.random() * (max + 1 - min));
    if (result > max) {
        result = max;
    }
    return result;
}


/**
 * 数组 按id分组
 * @param arr
 * @returns {Array}
 */
function arrGroup(arr) {
    // var arr = [{"id": "1001", "name": "值1", "value": "111"}, {
    //     "id": "1001",
    //     "name": "值1",
    //     "value": "11111"
    // }, {"id": "1002", "name": "值2", "value": "25462"}, {"id": "1002", "name": "值2", "value": "23131"}, {
    //     "id": "1002",
    //     "name": "值2",
    //     "value": "2315432"
    // }, {"id": "1003", "name": "值3", "value": "333333"}];
    //map用来判断是否已经存在组长 dest//存储分组后的数据
    var map = {}, dest = [];
    for (var i = 0; i < arr.length; i++) {
        var ai = arr[i];
        if (!map[ai.id]) {
            dest.push({
                dId: ai.id,
                dName: ai.name,
                data: [ai]
            });
            map[ai.id] = ai;
        } else {
            for (var j = 0; j < dest.length; j++) {
                if (dest[j].dId == ai.id) {
                    dest[j].data.push(ai);
                    break;
                }
            }
        }
    }
    return dest;
}

/**
 * 根据日期判断几天是周几
 * @param date
 * @returns {string}
 */
function getWeekDayByDate(date) {
    var weekDay = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
    var myDate = new Date(Date.parse(date));
    return weekDay[myDate.getDay() - 1];
}


/**
 *map转换为对象
 */
function strMapToObj(strMap) {
    let obj = Object.create(null);
    for (let [k, v] of strMap) {
        obj[k] = v;
    }
    return obj;
}

/**
 *map先转对象再转JSON
 */
function mapToJson(map) {
    return JSON.stringify(strMapToObj(map));
}

/**
 * 对象转map
 * @param obj
 * @returns {Map<any, any>}
 */
function objToStrMap(obj) {
    let strMap = new Map();
    for (let k of Object.keys(obj)) {
        strMap.set(k, obj[k]);
    }
    return strMap;
}

/**
 *json字符串转换为map
 */
function jsonToMap(jsonStr) {
    return objToStrMap(JSON.parse(jsonStr));
}

